﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data;

namespace Technoera.Common.Interop.OleDb
{
    public class OneToOneExportMapping : ExportMappingBase
    {
        public OneToOneExportMapping(string sourcePropertyName, string targetColumnName, DataTypeDefinition targetColumnDataType)
            : base(targetColumnName, targetColumnDataType)
        {
            SourcePropertyName = sourcePropertyName;
        }

        private string _sourcePropertyName;
        public string SourcePropertyName
        {
            get
            {
                if (_sourcePropertyName == null)
                    _sourcePropertyName = string.Empty;

                return _sourcePropertyName;
            }
            set
            {
                _sourcePropertyName = value;
            }
        }

	
        #region IExportMapping Members

        public override object GetPropertyValue(object instance)
        {
            Type sourceType = instance.GetType();
            if (SourcePropertyName.IndexOf('.') < 0)
            {
                PropertyInfo property = sourceType.GetProperty(SourcePropertyName);
                if (property == null) throw new TargetException(string.Format("Unable to find property '{0}' in Type '{1}'.", SourcePropertyName, sourceType.FullName));
                return property.GetValue(instance, null);
            }
            else
            {
                return ReflectionHelper.GetNestedPropertyValue(sourceType, SourcePropertyName, instance);
            }
        }

        #endregion
    }
}
